<div style="width:670px; float:left; margin-right:5px" class="border_radius">

<?php

if(!isset($_REQUEST['malsp'])):
	header("location:trangchu.php");
else:
	if(!is_numeric($_REQUEST['malsp']))
		header("location:trangchu.php");
	include_once("../../coms/xl_sanpham.php");
	$MaLoaiSanPham = $_REQUEST['malsp'];
	$MaThuongHieu = 0;
	//xac dinh bao nhieu dong
	$display = 10;
	// tinh tong so trang can hien thi
	if(!isset($_REQUEST['matt'])):
		$lt = new xl_sanpham();
		$arr_lt = $lt->Load_SanPham_TheoLoaiSanPham($MaLoaiSanPham);
		//Phan trang - START


		if(isset($_GET['page']) && (int)$_GET['page']) {
		$page = $_GET['page'];
		} else { //neu chua xac dinh, thi tim so trang
			$record = count($arr_lt);
			if($record > $display) {
				$page = ceil($record/$display);
			} else {
				$page = 1;
			}
		}
		$start = (isset($_GET['start']) && (int)$_GET['start']>=0) ? $_GET['start'] : 0;
		$tt = new xl_sanpham();
		$arr_sp = $tt->Load_SanPham_TheoLoaiSanPham_Limit($MaLoaiSanPham, $start, $display);
		//Phan trang -- END
	else:
		if(!is_numeric($_REQUEST['matt']))
			header("location:trangchu.php");
		$MaThuongHieu = $_REQUEST['matt'];
		$lt = new xl_sanpham();
		$arr_lt = $lt->Load_SanPham_TheoLoaiSanPham_ThuongHieu($MaLoaiSanPham,$MaThuongHieu);
		//Phan trang - START
		if(isset($_GET['page']) && (int)$_GET['page']) {
			$page = $_GET['page'];
			} else { //neu chua xac dinh, thi tim so trang
				$record = count($arr_lt);
				if($record > $display) {
					$page = ceil($record/$display);
				} else {
					$page = 1;
				}
		}
		$start = (isset($_GET['start']) && (int)$_GET['start']>=0) ? $_GET['start'] : 0;
		$tt = new xl_sanpham();
		$arr_sp = $tt->Load_SanPham_TheoLoaiSanPham_ThuongHieu_Limit($MaLoaiSanPham,$MaThuongHieu, $start, $display);

		//Phan trang -- END
	endif;
	if(isset($_REQUEST['act']) && isset($_REQUEST['malsp'])&& isset($_REQUEST['id'])):
		$act = $_REQUEST['act'];
		$id = $_REQUEST['id'];
		if($act == 'del' && is_numeric($id)):
			$sp = new xl_sanpham();
			$arr_sp = $sp->Load_SanPham_TheoMa($id);
			for($i=1; $i<=4; $i++):
				if($arr_sp['hinhanh'.$i] != ""):
					unlink("../../".$arr_sp['hinhanh'.$i]."");
				endif;
			endfor;
			$xoa = new xl_sanpham();
			$kq2 = $xoa->Xoa_SanPham($id);
			echo '<script type="text/javascript">
						alert("Xóa sản phẩm thành công!!!");
					</script>';
			echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=qlctlsp&malsp='.$MaLoaiSanPham.'"  />';
		endif;

	endif;

?>

<div class="TitlePart">
	<?php
		include_once("../../coms/xl_loaisanpham.php");
		$lsp = new xl_loaisanpham();
		$arr_lsp = $lsp->Load_LoaiSanPham_TheoMa($MaLoaiSanPham);
	?>
    <h3> <span style="color:#006">Quản lý sản phẩm "<?php echo $arr_lsp['tenloaisanpham'];?>"</span>
    	<a href="trangchu.php?key=t_sp&malsp=<?php echo $_REQUEST['malsp'];?>" class="button medium blue" style="float:right; margin-right:5px; margin-top:2px; margin-bottom:10px">Thêm sản phẩm</a>
    </h3>
</div>

<div id="list_tin_moi" style="padding-bottom:5px; border:0;">
    <div style="display:block; text-align:right">
        <div align="left" style="padding-bottom:5px; padding-left:5px; vertical-align:text-top">
	<?php
		$th = new xl_sanpham();
		$arr_th = $th->Load_ThuongHieu_TheoLoaiSanPham($MaLoaiSanPham);
		for ($j=0; $j<count($arr_th); $j++):
        	$row_th = $arr_th[$j];
	?>
    		<a href="trangchu.php?key=qlctlsp&malsp=<?php echo $MaLoaiSanPham?>&matt=<?php echo $row_th['mathuonghieu']?>">
				<img src="../../<?php echo $row_th['hinhanh']?>" height="25" />
            </a>&nbsp;(<?php echo $row_th['soluong']?>)&nbsp;
    <?php
		endfor;

	?>
    </div>

    </div>

    <table width="100%" cellspacing="0" style="padding-left:5px; padding-right:5px;"  id="myTable">

        <tr bgcolor="#999999">
            <th align="center" valign="middle">
                STT
            </th>
           <th align="center" valign="middle">
               Hình
           </th>
            <th align="center" valign="middle">
                Tên sản phẩm
            </th>
            <th align="center" valign="middle">
                Chức năng
            </th>

        </tr>

    <?php
    for ($i=0; $i<count($arr_sp); $i++):
        $row = $arr_sp[$i];

    ?>

        <tr>

            <td align="center" valign="middle">

                <?php echo $start+ $i+1; ?>

            </td>
            <td align="center" valign="middle">
            	<?php
					if($row['hinhanh1'] != ""):
						$HinhAnhSP = $row['hinhanh1'];
					else:
						$HinhAnhSP = "images/sanpham/no_photo.jpg";
					endif;
				?>
                <a href="../../<?php echo $HinhAnhSP;?>" title="<?php echo $row['tensanpham']; ?>" rel="image_group">
                   <img src="../../<?php echo $HinhAnhSP;?>" border="0" width="50" height="50"/>
                </a>

            </td>

            <td align="justify" valign="middle" style="padding-left:5px; color:#333">

                <strong><?php echo $row['tensanpham']; ?></strong><br>
                <span style="font-size:13px">- Thương hiệu: </span><img src="../../<?php echo $row['hinhanh']; ?>" height="15"><br>
                <span style="font-size:13px">- Công suất: <?php echo $row['tencongsuat']; ?> 	</span><br />

				<span style="font-size:13px">- Giá tiền: 	</span>
				<?php
					if($row['gia'] == 0):
						echo '<span style="color:#F00; font-weight:bold;">Call</span>';
					else:
						echo '<span style="color:#F00; font-weight:bold;">'.number_format($row['gia']).'</span>';
					endif;

				?><br>


            </td>

            <td align="center" valign="middle">

                <a href="trangchu.php?key=s_sp&id=<?php echo $row['masanpham']; ?>" class="button medium blue">Chi tiết</a>

                <a href="trangchu.php?key=qlctlsp&act=del&malsp=<?php echo $row['maloaisanpham']; ?>&id=<?php echo $row['masanpham']; ?>" class="button medium blue" onclick="javascript: return confirm('Bạn chắc muốn xóa sản phẩm này không?');">Xóa</a>

            </td>

        </tr>

    <?php

    endfor;

    ?>



    </table><br />
	<!--Phan trang - START-->
        <ul class="nav">
        <?php
            if($page > 1) { //neu can hien thi so trang

                $next = $start + $display;
                $prev = $start - $display;
                $current = ($start/$display)+1;
                //Hien thi trang Previous
                if($current !=1) {
					if($MaThuongHieu != 0):
						echo "<li><a href='trangchu.php?key=qlctlsp&malsp=$MaLoaiSanPham&matt=$MaThuongHieu&start=$prev&page=$page'>Trước</a></li>";
					else:
               			 echo "<li><a href='trangchu.php?key=qlctlsp&malsp=$MaLoaiSanPham&start=$prev&page=$page'>Trước</a></li>";
					endif;
                }

                if($current >= 4):
                    if($current + 6 >= $page)
                        $limit = $page;
                    else
                        $limit = $current + 6;
                    for($i=$current - 2;$i<=$limit;$i++) {
                        if($current != $i) {
							if($MaThuongHieu != 0):
								 echo "<li><a href='trangchu.php?key=qlctlsp&malsp=$MaLoaiSanPham&matt=$MaThuongHieu&start=".($display*($i-1))."&page=$page'>$i</a></li>";
							else:
								 echo "<li><a href='trangchu.php?key=qlctlsp&malsp=$MaLoaiSanPham&start=".($display*($i-1))."&page=$page'>$i</a></li>";
							endif;

                        } else {
                            echo "<li class='current'><a href='#'>$i</a></li>";
                        }
                        if(($i == $limit) && ($i < $page)){
                            echo "<li><a href='#'>...</a></li>";
                        }
                    }

                else:
					//toi da hien thi 5 trang
					 if($page < 5)
                        $limit = $page;
                    else
                        $limit = 5;
                    //Hien thi so link
                    for($i=1;$i<=$limit;$i++):
                        if($current != $i) {
							if($MaThuongHieu != 0):
								echo "<li><a href='trangchu.php?key=qlctlsp&malsp=$MaLoaiSanPham&matt=$MaThuongHieu&start=".($display*($i-1))."&page=$page'>$i</a></li>";
							else:
								  echo "<li><a href='trangchu.php?key=qlctlsp&malsp=$MaLoaiSanPham&start=".($display*($i-1))."&page=$page'>$i</a></li>";
							endif;

                        } else {
                            echo "<li class='current'><a href='#'>$i</a></li>";
                        }
                        if(($i == 5) && ($i < $page)){
                            echo "<li><a href='#'>...</a></li>";
                        }
                    endfor; //End: FOR
                endif;

                //Hien thi trang Next

                if($current != $page) {
					if($MaThuongHieu != 0):
						echo "<li><a href='trangchu.php?key=qlctlsp&malsp=$MaLoaiSanPham&matt=$MaThuongHieu&start=$next&page=$page'>Sau</a></li>";
					else:
						  echo "<li><a href='trangchu.php?key=qlctlsp&malsp=$MaLoaiSanPham&start=$next&page=$page'>Sau</a></li>";
					endif;

                }

            }//End: $page > 1 IF
        ?>
        </ul>
	<!--Phan trang - END-->




</div>

<?php

endif;

?>

</div>



